package com.hskn.hss.module.operatedauthor.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.button.entity.SysButton;
import com.hskn.hss.module.button.service.ISysButtonService;
import com.hskn.hss.module.menu.entity.SysMenu;
import com.hskn.hss.module.menu.service.ISysMenuService;
import com.hskn.hss.module.menu.service.impl.SysMenuServiceImpl;
import com.hskn.hss.module.operatedauthor.entity.SysRoleMenu;
import com.hskn.hss.module.operatedauthor.mapper.SysRoleMenuMapper;
import com.hskn.hss.module.operatedauthor.service.ISysRoleMenuService;
import com.hskn.hss.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-04
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {
    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    ISysMenuService sysMenuService;
    @Resource
    ISysButtonService sysButtonService;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {
        SysRoleMenu sysRoleMenu = params.getObject("data", SysRoleMenu.class);
        sysRoleMenu.setId(StringUtils.uuid());
        int i = sysRoleMenuMapper.insert(sysRoleMenu);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult deleteByid(JSONObject params) throws HssException {
        String id = params.getString("id");
        SysRoleMenu sysRoleMenu = sysRoleMenuMapper.selectById(id);
        int i = sysRoleMenuMapper.updateById(sysRoleMenu);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {
        SysRoleMenu sysRoleMenu = params.getObject("data", SysRoleMenu.class);
        int i = sysRoleMenuMapper.updateById(sysRoleMenu);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public SysRoleMenu getInfoById(JSONObject params) throws HssException {
        String id = params.getString("id");
        SysRoleMenu sysRoleMenu = sysRoleMenuMapper.selectById(id);
        return sysRoleMenu;
    }

    @Override
    public List<SysRoleMenu> getListByMap(JSONObject params) throws HssException {
        Map map = new HashMap();
        map = params.getObject("queryparams", Map.class);
        List<SysRoleMenu> RoleMenusList = sysRoleMenuMapper.selectByMap(map);
        return RoleMenusList;
    }

    @Override
    public void deleteByMap(String roleid) throws HssException {
        Map map = new HashMap();
        map.put("roleid", roleid);
        sysRoleMenuMapper.deleteByMap(map);
    }
    @Override
    public List<SysMenu> getMenuListByRoleIds(List<String> roleIds) {
        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuMapper.selectList(new QueryWrapper<SysRoleMenu>().lambda().in(SysRoleMenu::getRoleid,roleIds));
        List<SysMenu> sysMenuList = sysMenuService.selectList(new QueryWrapper<SysMenu>().lambda().eq(SysMenu::getStatus,0).in(
                SysMenu::getId,sysRoleMenuList.stream().map(SysRoleMenu::getMenuid).collect(Collectors.toList())));
        return sysMenuList;
    }
    @Override
    public List<SysButton> getButtonByRoleIds(List<String> roleIds) {
        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuMapper.selectList(new QueryWrapper<SysRoleMenu>().lambda().in(SysRoleMenu::getRoleid,roleIds).isNotNull(SysRoleMenu::getButtonid));
        if (CollectionUtils.isNotEmpty(sysRoleMenuList)) {
            return sysButtonService.selectList(new QueryWrapper<SysButton>().lambda().in(SysButton::getId,sysRoleMenuList.stream().map(SysRoleMenu::getButtonid).collect(Collectors.toList())));
        }else {
            return new ArrayList<>();
        }
    }
    @Override
    public List<SysRoleMenu> selectList(Wrapper<SysRoleMenu> wrapper) {
        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuMapper.selectList(wrapper);
        return sysRoleMenuList;
    }
}
